<template>
    <div class="room-cash-box">
        <div class="lists">
            <div class="item" v-for="(item, k) of lists">
                <div class="time">{{item.time}}</div>
                <div class="body">
                    <div class="info">
                        <div class="user">
                            &#xe604; {{item.user.user_name}}
                        </div>
                        <div class="gold" style="color: #FF8D24">&#xe934; <span>{{item.number}}</span></div>
                    </div>
                    <div class="act">
                        <div class="btn" v-if="item.status === 0">
                            <mt-button
                                    class="mtbtn"
                                    size="small"
                                    @click="confirm(k)"
                                    type="primary">
                                <span v-if="!isLoading(k)">确定</span>
                                <mt-spinner
                                        v-else
                                        type="fading-circle"
                                        color="white"></mt-spinner>
                            </mt-button>
                        </div>
                        <span class="success" v-else-if="item.status === 1">已完成</span>
                        <span class="cancel" v-else>已取消</span>
                    </div>
                </div>
            </div>
        </div>
        <div class="page" v-if="!loading && !nullData" @click="loadMore">下一页</div>
        <div class="loading" v-if="loading">数据加载中....</div>
        <div class="null" v-if="nullData">没有更多了</div>
    </div>
</template>

<script>
    import apiUrl from '@/api/apiUrl'
    import {Toast} from 'mint-ui'
    export default {
        name: "index",
        data() {
            return {
                loading: false,
                lists: [],
                nullData: false,
                page: 1,
                listsLoading: []
            }
        },
        beforeRouteEnter(to, from, next) {
            next(vm => {
                vm.setData(to.query)
            })
        },
        methods: {
            loadMore(){
                this.setData(this.$route.query)
            },
            confirm(k) {

                let c = confirm('确定要返现吗?')

                if (! c) {
                    return false
                }

                let params = {
                    params: {
                        id: this.lists[k].id
                    }
                }

                this.$http.get(apiUrl.room.cashConfirm, params).then(res=> {

                    if (res.data.code === 0) {
                        this.lists[k].status = 1
                        Toast('已提现')
                    } else {
                        Toast('提现失败')
                    }

                    let arr = this.listsLoading.filter(v => {
                        return v !== k
                    })
                    this.listsLoading = arr
                }).catch(err => {
                    let arr = this.listsLoading.filter(v => {
                        return v !== k
                    })
                    this.listsLoading = arr
                    console.log(err)
                })
            },
            isLoading(k) {
                return this.listsLoading.some(v => {
                    return v === k
                })
            },
            setData(q) {
                this.loading = true
                let params = {
                    params: {
                        room_id: this.$route.query.room_id,
                        page: this.page
                    }
                }
                this.$http.get(apiUrl.room.cashLists, params).then(res => {
                    if (res.data.code === 0) {
                        res.data.result.forEach(v => {
                            this.lists.push(v)
                        })
                        if (res.data.result.length < 20) {
                            this.nullData = true
                        }
                    } else {
                        Toast(res.data.msg)
                    }
                    this.loading = false
                }).catch(err => {
                    this.loading = false
                    console.log(err)
                })
                this.page++
            }
        }
    }
</script>

<style scoped lang="less">
    @import "../../../assets/css/bases";
    .lists{
        .item {
            padding: @default;
            border-bottom: #f1f1f1 solid 6px;
            .time {
                color: @gray;
            }
            .body {
                display: flex;
                justify-content: space-between;
                align-items: center;
                .info {
                    line-height: 25px;
                }
                .act {
                    .mtbtn {
                        height: 25px;
                    }
                    .success {
                        color: #55a532;
                    }
                    .cancel{
                        color: @gray;
                    }
                }
            }
        }
    }
    .page, .loading, .null {
        text-align: center;
        line-height: 45px;
    }
    .loading, .null {
        color: @gray;
    }
</style>